/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.utils
{
	import flash.events.Event;
	import flash.net.URLRequest;
	
	/**
	 * 
	 * @author puny
	 * @example
	 * <listing version="3.0">
	 * var linkageIDs:LinkageIDs = new LinkageIDs("myAssets.swf");
	 * linkageIDs.addEventListener(FOUND_SYMBOL_CLASS, foundHandler);
	 * 
	 * function foundHandler(event:Event):void {
	 * 		for each(var linkageID:String in linkageIDs.symbolClassNames()) {
	 * 			trace(linkageID);
	 * 		}
	 * }
	 * </listing>
	 */
	public class LinkageIDs extends SwfParser
	{
		public static const FOUND_SYMBOL_CLASS:String = "found symbol class";
		public static const TAG_EXPORT_ASSETS:String = "56";
		public static const TAG_SYMBOL_CLASS:String = "76";
		private const EXPORT_ASSETS:Array = new Array();
		public function LinkageIDs(request:URLRequest=null)
		{
			super(request);
		}
		
		override protected function parseTag(type:String):Boolean {
			if (type == TAG_SYMBOL_CLASS) {
				parseSymbolClass();
				return true;
			}
			return super.parseTag(type);
		}
		
		/**
		 * 
		 * @return Array of found export names (Linkage IDs) 
		 * 
		 */
		public function symbolClassNames():Array {
			return EXPORT_ASSETS;
		}
		
		private function parseSymbolClass():void {
			var numOfSymbols:uint = readUI(16);// _swf.readUnsignedByte() + _swf.readUnsignedByte() * 0x100;
			for(var i:int =0;i < numOfSymbols;i++) {
				var id:uint = readUI(16);//_swf.readUnsignedByte() + _swf.readUnsignedByte() * 0x100;
				var ch:uint;
				var symbolName:String = "";
				while((ch = _swf.readUnsignedByte()) != 0) {
					symbolName += String.fromCharCode(ch);
				}
				EXPORT_ASSETS.push(symbolName);
			}
			if (EXPORT_ASSETS.length > 0) {
				dispatchEvent(new Event(FOUND_SYMBOL_CLASS));
			}
		}
	}
}